"use strict";
$jq.namespace('PeopleMedia.CP');

PeopleMedia.CP.GeoManager = function () {
	this._countries = [
		{code:"US",name:"United States"},
		{code:"CA",name:"Canada"},
		{code:"AL",name:"Albania"},
		{code:"DZ",name:"Algeria"},
		{code:"AR",name:"Argentina"},
		{code:"AM",name:"Armenia"},
		{code:"AU",name:"Australia"},
		{code:"AT",name:"Austria"},
		{code:"AZ",name:"Azerbaijan"},
		{code:"BH",name:"Bahrain"},
		{code:"BY",name:"Belarus"},
		{code:"BE",name:"Belgium"},
		{code:"BZ",name:"Belize"},
		{code:"BO",name:"Bolivia"},
		{code:"BA",name:"Bosnia and Herzegovina"},
		{code:"BR",name:"Brazil"},
		{code:"BN",name:"Brunei Darussalam"},
		{code:"BG",name:"Bulgaria"},
		{code:"CA",name:"Canada"},
		{code:"CL",name:"Chile"},
		{code:"CO",name:"Colombia"},
		{code:"CR",name:"Costa Rica"},
		{code:"HR",name:"Croatia"},
		{code:"CZ",name:"Czech Republic"},
		{code:"DK",name:"Denmark"},
		{code:"DO",name:"Dominican Republic"},
		{code:"EC",name:"Ecuador"},
		{code:"EG",name:"Egypt"},
		{code:"SV",name:"El Salvador"},
		{code:"EE",name:"Estonia"},
		{code:"FO",name:"Faroe Islands"},
		{code:"FI",name:"Finland"},
		{code:"FR",name:"France"},
		{code:"GE",name:"Georgia"},
		{code:"DE",name:"Germany"},
		{code:"GR",name:"Greece"},
		{code:"GT",name:"Guatemala"},
		{code:"HN",name:"Honduras"},
		{code:"HK",name:"Hong Kong S.A.R."},
		{code:"HU",name:"Hungary"},
		{code:"IS",name:"Iceland"},
		{code:"IN",name:"India"},
		{code:"ID",name:"Indonesia"},
		{code:"IR",name:"Iran"},
		{code:"IQ",name:"Iraq"},
		{code:"IE",name:"Ireland"},
		{code:"PK",name:"Islamic Republic of Pakistan"},
		{code:"IL",name:"Israel"},
		{code:"IT",name:"Italy"},
		{code:"JM",name:"Jamaica"},
		{code:"JP",name:"Japan"},
		{code:"JO",name:"Jordan"},
		{code:"KZ",name:"Kazakhstan"},
		{code:"KE",name:"Kenya"},
		{code:"KR",name:"Korea"},
		{code:"KW",name:"Kuwait"},
		{code:"KG",name:"Kyrgyzstan"},
		{code:"LV",name:"Latvia"},
		{code:"LB",name:"Lebanon"},
		{code:"LY",name:"Libya"},
		{code:"LI",name:"Liechtenstein"},
		{code:"LT",name:"Lithuania"},
		{code:"LU",name:"Luxembourg"},
		{code:"MO",name:"Macao S.A.R."},
		{code:"MK",name:"Macedonia (FYROM)"},
		{code:"MY",name:"Malaysia"},
		{code:"MV",name:"Maldives"},
		{code:"MT",name:"Malta"},
		{code:"MX",name:"Mexico"},
		{code:"MN",name:"Mongolia"},
		{code:"MA",name:"Morocco"},
		{code:"NL",name:"Netherlands"},
		{code:"NZ",name:"New Zealand"},
		{code:"NI",name:"Nicaragua"},
		{code:"NO",name:"Norway"},
		{code:"OM",name:"Oman"},
		{code:"PA",name:"Panama"},
		{code:"PY",name:"Paraguay"},
		{code:"CN",name:"People's Republic of China"},
		{code:"PE",name:"Peru"},
		{code:"PL",name:"Poland"},
		{code:"PT",name:"Portugal"},
		{code:"MC",name:"Principality of Monaco"},
		{code:"PR",name:"Puerto Rico"},
		{code:"QA",name:"Qatar"},
		{code:"PH",name:"Republic of the Philippines"},
		{code:"RO",name:"Romania"},
		{code:"RU",name:"Russia"},
		{code:"SA",name:"Saudi Arabia"},
		{code:"CS",name:"Serbia"},
		{code:"SG",name:"Singapore"},
		{code:"SK",name:"Slovakia"},
		{code:"SI",name:"Slovenia"},
		{code:"ZA",name:"South Africa"},
		{code:"ES",name:"Spain"},
		{code:"SE",name:"Sweden"},
		{code:"CH",name:"Switzerland"},
		{code:"SY",name:"Syria"},
		{code:"TW",name:"Taiwan"},
		{code:"TH",name:"Thailand"},
		{code:"TT",name:"Trinidad and Tobago"},
		{code:"TN",name:"Tunisia"},
		{code:"TR",name:"Turkey"},
		{code:"AE",name:"U.A.E."},
		{code:"UA",name:"Ukraine"},
		{code:"GB",name:"United Kingdom"},
		{code:"US",name:"United States"},
		{code:"UY",name:"Uruguay"},
		{code:"UZ",name:"Uzbekistan"},
		{code:"VE",name:"Venezuela"},
		{code:"VN",name:"Vietnam"},
		{code:"YE",name:"Yemen"},
		{code:"ZW",name:"Zimbabwe"}
	];
};

PeopleMedia.CP.GeoManager.prototype = {
    Oid: "PeopleMedia.CP.GeoManager",
    Countries: function () {
        return this._countries;
    },
    loadDDL: function (ddlId) {
        try {
	        try {
	        	var ddlContainer = $jq('#'+ddlId+'Container');
	        	if (ddlContainer.length > 0 && ddlContainer.is('div')) {
	        	    this.loadDDL_Div(ddlId);
	        	} else {
	        	    this.loadDDL_Dropdown(ddlId);
	        	}
	        }
	        catch (ex) {
	            PeopleMedia.CP.handleException(ex);
            	this.loadDDL_Dropdown(ddlId);
	        }
        }
        catch (ex) {
            PeopleMedia.CP.handleException(ex, { oid:this.Oid, method:"loadDDL" });
        }
    },
    loadDDL_Dropdown: function (ddlId) {
        try {
            var ddl = document.getElementById(ddlId);
            $jq.each(this._countries, function (idx, country) {
                var o = document.createElement("option");
                o.text = country.name;
                o.value = country.code;
                try {
                    ddl.add(o, null);
                } catch (ex) { // ie only
                    ddl.add(o);
                }
            });
        }
        catch (ex) {
            PeopleMedia.CP.handleException(ex, { oid:this.Oid, method:"loadDDL_Dropdown" });
        }
    },
    loadDDL_Div: function (ddlId) {
        try {
            var ddlContainer = $jq('#'+ddlId+'Container');

            var templateItemName = ddlId + 'ItemTemplate';

			var template = $jq('<div>').append($jq('#'+ddlId+'ItemTemplate').clone());
			template.children().css('display','');
			var templateHtml = template.html();

        	var html = "";
            $jq.each(this._countries, function (idx, country) {
            	html += templateHtml.replace("##COUNTRYNAME##",country.name).replace("##COUNTRYCODE##",country.code).replace(templateItemName,ddlId+'Item'+country.code);
            });
            ddlContainer.html(html);
        }
        catch (ex) {
            PeopleMedia.CP.handleException(ex, { oid:this.Oid, method:"loadDDL_Div" });
        }
    }
};
